package com.lp.easymethodlink;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.psi.*;
import com.lp.easymethodlink.model.CallHierarchyNode;
import com.lp.easymethodlink.service.CallHierarchyService;
import com.lp.easymethodlink.ui.CallHierarchyToolWindowPanel;

public class MethodCallHierarchyAction extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent e) {
        // 获取当前项目和编辑器
        Project project = e.getProject();
        Editor editor = e.getData(CommonDataKeys.EDITOR);
        
        if (project == null || editor == null) {
            Messages.showMessageDialog("无法获取项目或编辑器信息", "错误", Messages.getErrorIcon());
            return;
        }
        
        // 获取当前文件和光标位置
        PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE);
        if (psiFile == null) {
            Messages.showMessageDialog("无法获取当前文件", "错误", Messages.getErrorIcon());
            return;
        }
        
        // 获取光标所在元素
        int offset = editor.getCaretModel().getOffset();
        PsiElement element = psiFile.findElementAt(offset);
        
        if (element == null) {
            Messages.showMessageDialog("无法定位光标所在元素", "错误", Messages.getErrorIcon());
            return;
        }
        
        // 查找包含该元素的方法
        PsiMethod method = findContainingMethod(element);
        if (method == null) {
            Messages.showMessageDialog("请将光标放置在方法内部", "提示", Messages.getInformationIcon());
            return;
        }
        
        // 显示方法信息调试
        String methodInfo = method.getContainingClass().getName() + "." + method.getName();
        System.out.println("Starting analysis for method: " + methodInfo);
        
        // 分析调用链路
        CallHierarchyService service = new CallHierarchyService(project);
        CallHierarchyNode rootNode = service.buildCallHierarchy(method);
        
        // 获取工具窗口并显示结果
        ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(project);
        ToolWindow toolWindow = toolWindowManager.getToolWindow("CallHierarchy");
        
        if (toolWindow != null) {
            // 激活工具窗口
            toolWindow.activate(null);
            
            // 获取工具窗口面板并显示结果
            CallHierarchyToolWindowPanel panel = project.getUserData(CallHierarchyToolWindowPanel.KEY);
            if (panel != null) {
                panel.displayCallHierarchy(rootNode);
            }
        } else {
            Messages.showMessageDialog("无法打开调用链路工具窗口", "错误", Messages.getErrorIcon());
        }
        
        // 显示调试信息
        System.out.println("Analysis completed for method: " + methodInfo);
        System.out.println("Root node children count: " + rootNode.getChildren().size());
    }
    
    /**
     * 查找包含指定元素的方法
     * @param element PSI元素
     * @return 包含该元素的方法，如果未找到则返回null
     */
    private PsiMethod findContainingMethod(PsiElement element) {
        while (element != null) {
            if (element instanceof PsiMethod) {
                return (PsiMethod) element;
            }
            element = element.getParent();
        }
        return null;
    }
    
    @Override
    public void update(AnActionEvent e) {
        // 只有在编辑器中有文件时才启用此操作
        Project project = e.getProject();
        Editor editor = e.getData(CommonDataKeys.EDITOR);
        e.getPresentation().setEnabledAndVisible(project != null && editor != null);
    }
}